package com.alm.token.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alm.token.ConStant;
import com.alm.token.R;
import com.alm.token.base.BaseActivity;
import com.alm.token.base.BaseAdapter;
import com.alm.token.common.RefreshLoadMoreManager;
import com.alm.token.ui.data.bean.MyIncomeData;
import com.alm.token.ui.data.bean.MyIncomeLogData;
import com.alm.token.ui.persenter.MyPreenter;
import com.alm.token.ui.view.ShouyiView;
import com.alm.token.utils.ActionBarHelper;
import com.alm.token.utils.CommonUtil;
import com.alm.token.utils.EmptyUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import baibeiwallet.Constant;
import butterknife.BindView;
import butterknife.OnClick;

public class MyFortuneActivity extends BaseActivity implements ShouyiView<Object> {
    @BindView(R.id.recycler_view)
    RecyclerView recyclerView;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_clickreload)
    TextView tvClickreload;
    @BindView(R.id.nodatashow_body)
    RelativeLayout nodatashowBody;
    @BindView(R.id.tv_joinvip)
    TextView tvJoinvip;

    @BindView(R.id.tv_today_totalshouyi)
    TextView tvTodayTotalshouyi;
    @BindView(R.id.tv_mioprice)
    TextView tvMioprice;
    @BindView(R.id.tv_totayzhi)
    TextView tvTotayzhi;
    @BindView(R.id.tv_huobanzhi)
    TextView tvHuobanzhi;
    @BindView(R.id.rly_shouyitop1)
    RelativeLayout lyShouyiTop;
    @BindView(R.id.rly_shouyitop2)
    LinearLayout lyShouyiTop2;

    private RefreshLoadMoreManager<MyIncomeData.DataBean.ListBean> manager;
    private List<MyIncomeData.DataBean.ListBean> dataBeans = new ArrayList<>();

    @Override
    protected MyPreenter initPreenter() {
        return new MyPreenter(this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_myfortune;
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews() {
        new ActionBarHelper(this).setTitle(getString(R.string.quantitative_hint_myfortune)).setRightText(getString(R.string.fortune_hint_myhostoryorder)).setRightListener(this);
        tvJoinvip.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(VipApplyActivity.class);
//                startActivity(VipApplySplashActivity.class);
            }
        });
        lyShouyiTop.setVisibility(View.GONE);
        lyShouyiTop2.setVisibility(View.GONE);
        swipeRefreshLayout.setVisibility(View.GONE);
        initManager();
    }

    private void initManager() {
        GroupAdapter adapter = new GroupAdapter(this);
        manager = new RefreshLoadMoreManager.Builder(this)
                .setAdapter(adapter)
                .setRecyclerView(recyclerView)
                .setSwipeRefreshLayout(swipeRefreshLayout)
                .setOnRefreshListener(new RefreshLoadMoreManager.OnRefreshListener() {
                    @Override
                    public void onRefresh() {
//                        showToast("onRefresh");
                        swipeRefreshLayout.setRefreshing(false);
                        loadData();
                    }
                })
                .build();
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

//                if (EmptyUtils.isNotEmpty(dataBeans) && dataBeans.size() > position) {
//                    Intent intent = new Intent(MyFortuneActivity.this, ShouyiListLogActivity.class);
//                    intent.putExtra("channelType", String.valueOf(dataBeans.get(position).getId()));
//                    startActivity(intent);
//                }


            }
        });
        recyclerView.setNestedScrollingEnabled(false);
    }

    @Override
    protected void loadData() {
        preenter.getMyIncomeData("AGO");
//        manager.setData(getTestData());
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rightItem:
//                    Intent intent = new Intent(this, MyFortuneListLogActivity.class);
//                    intent.putExtra("channelType", String.valueOf(dataBeans.get(position).getId()));
//                    startActivity(intent);
                startActivity(MyFortuneListLogActivity.class);
                break;
            case R.id.tv_mioprice:
//                showToast("click " + getString(R.string.fortune_hint_donthaveanyfortune));
                break;
        }
    }

    @Override
    public void getData(Object o) {

    }

    @Override
    public void jumpToLoginAct(int staue) {
        if (staue == 3) {
//            showToast(getString(R.string.common_relogin));
//            Intent intent = new Intent();
//            intent.setAction(ConStant.EXIT_APP);
//            sendBroadcast(intent);
//            startActivity(BlockWalletLoginActivity.class);
//            finish();
        } else if (staue == 4) {
//            showToast(getString(R.string.paypasw_hint_setpaypsw));
//            Intent intent = new Intent(getActivity(),PayPwdActivity.class);
//            intent.putExtra("PayPwdActivityType",PayPwdActivity.PAYPSW_SET);
//            startActivity(intent);
//            getActivity().finish();
            showToSetPaypswDialog();
        } else if (staue == 666) {
            showToUpdata();
        }
    }

    @Override
    public void onError(String error) {

        showToast(getString(R.string.common_loaderrritry));
    }

    @Override
    public void ShowProgress() {
        showWaitDialog();
    }

    @Override
    public void hideProgress() {
        hideWaitDialog();
    }

    @Override
    public void getmyIncomeData(MyIncomeData myIncomeData) {

        if (myIncomeData.getStatus() == 0 && myIncomeData.getData() != null) {


            MyIncomeData.DataBean data = myIncomeData.getData();
//            BigDecimal bigDecimal = new BigDecimal(data.getTeamValue());
//            data.setTeamValue(bigDecimal.toString());
            tvTodayTotalshouyi.setText("¥" + CommonUtil.saveToPoint2WithNo(data.getTodayMoney()));
            tvMioprice.setText(getString(R.string.fortune_hint_leijishouyi, String.valueOf(data.getPrice())) + " " + Constant.G12ContractName);
            tvTotayzhi.setText(String.valueOf(data.getTodayValue()) + "$");
//            tvHuobanzhi.setText(data.getTeamValue() + "" + "$");
            lyShouyiTop.setVisibility(View.VISIBLE);
            lyShouyiTop2.setVisibility(View.VISIBLE);
            tvMioprice.setOnClickListener(this);
//            dataBeans = data.getList();
//
//            if (dataBeans != null) {
//                for (int i = 0; i < dataBeans.size(); i++) {
//                    dataBeans.get(i).setLayoutId(GroupAdapter.LAYOUT_ID);
//                }
//                manager.setData(dataBeans);
//            }
            if (dataBeans.size() == 0) {
                swipeRefreshLayout.setVisibility(View.GONE);
                nodatashowBody.setVisibility(View.VISIBLE);
                tvClickreload.setText(getString(R.string.fortune_hint_donthaveanyfortune));
            } else {
                swipeRefreshLayout.setVisibility(View.VISIBLE);
                nodatashowBody.setVisibility(View.GONE);
            }
        } else showToast(myIncomeData.getMsg());

    }

    @Override
    public void getmyIncomeLogData(MyIncomeLogData myIncomeLogData) {

    }

    @OnClick({R.id.tv_clickreload})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_clickreload:
//                loadData();
                showToast("click " + getString(R.string.fortune_hint_donthaveanyfortune));
                break;
        }

    }


    public class GroupAdapter extends BaseAdapter<MyIncomeData.DataBean.ListBean> {

        public static final int LAYOUT_ID = R.layout.item_earningslist;

        public GroupAdapter(@NonNull Context context) {
            super(context);
        }

        @Override
        protected void convert(BaseViewHolder helper, MyIncomeData.DataBean.ListBean item, int itemType) {

//            ImageView icon = helper.getView(R.id.icon);
//            String url = "https://tvax3.sinaimg.cn/crop.0.0.996.996.180/56303b98ly8fndrnuk3z6j20ro0rodjg.jpg";
//            GlideApp.with(ShouyiListActivity.this).load(url).circleCrop().into(icon);  CommonUtil.getDateTimeFromMillisecond
            helper.setText(R.id.tv_shouyineame, item.getName());
            helper.setText(R.id.tv_shouyimoney, String.valueOf(item.getMoney()));
            helper.setBackgroundRes(R.id.icon, item.getId() == 4 ? R.mipmap.icon_earningslist_item1 :
                    item.getId() == 5 ? R.mipmap.icon_earningslist_item2 :
                            item.getId() == 6 ? R.mipmap.icon_earningslist_item3 :
                                    item.getId() == 7 ? R.mipmap.icon_earningslist_item5 : R.mipmap.icon_earningslist_item4);
        }

    }
}
